package com.imist.jdbc.dao;

import com.imist.jdbc.bean.User;
import com.imist.jdbc.common.ConnectionUtil;

import java.sql.*;

public class UserDao {
    public User login(String username,String password){
        Connection conn = ConnectionUtil.getConnection();
        String sql = "select * From user where username = ? and password = ?";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            statement = conn.prepareStatement(sql);
            statement.setString(1,username);
            statement.setString(2,password);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                user = new User();
                user.setId(resultSet.getLong("id"));
                user.setName(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setRealName(resultSet.getString("real_name"));
                user.setBirthday(resultSet.getDate("birthday"));
                user.setPhone(resultSet.getString("phone"));
                user.setAddress(resultSet.getString("address"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionUtil.release(conn,statement,resultSet);
        }
        return null;
    }

    public User getUserById(long id) {
        Connection conn = ConnectionUtil.getConnection();
        String sql = "select * From user where id = ?";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            statement = conn.prepareStatement(sql);
            statement.setLong(1,id);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                user = new User();
                user.setId(resultSet.getLong("id"));
                user.setName(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setRealName(resultSet.getString("real_name"));
                user.setBirthday(resultSet.getDate("birthday"));
                user.setPhone(resultSet.getString("phone"));
                user.setAddress(resultSet.getString("address"));
                return user;
            }
        } catch (SQLException e) {
            System.out.println("查询用户信息失败");
            e.printStackTrace();
        }finally {
            ConnectionUtil.release(conn,statement,resultSet);
        }
        return null;
    }

    public boolean updateUser(User user) {
        Connection conn = ConnectionUtil.getConnection();
        String sql = "UPDATE user SET username = ? ,password = ? ,real_name = ? ,birthday = ?,phone =?,address = ? WHERE id=?";
        PreparedStatement statement = null;
        try {
            statement = conn.prepareStatement(sql);
            statement.setString(1,user.getName());
            statement.setString(2,user.getPassword());
            statement.setString(3,user.getRealName());
            statement.setDate(4,new Date(user.getBirthday().getTime()));
            statement.setString(5,user.getPhone());
            statement.setString(6,user.getAddress());
            statement.setLong(7,user.getId());
            statement.executeUpdate();
        } catch (SQLException e) {
            System.out.println("查询用户信息失败");
            e.printStackTrace();
            return false;
        }finally {
            ConnectionUtil.release(conn,statement,null);
        }
        return true;
    }
}
